//
//  ListCollectionView.m
//  Cat
//
//  Created by lushitong on 2019/1/14.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "ListCollectionView.h"
#import "ListCollectionReusableView.h"
#import "ShopCollectionCell.h"
#import "HttpAssottManager.h"
#import "ImageCollectionViewCell.h"

static NSString *const cellID = @"FLHomeCollectionViewCell";

@interface ListCollectionView()

@property (nonatomic, strong) NSNumber *pid;
@property (nonatomic, strong) NSMutableArray *productList;

@end

@implementation ListCollectionView

- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewFlowLayout *)layout {
	self = [super initWithFrame:frame layout:layout];
	if (self) {
		self.addRefresh = NO;
		self.needLoadMore = NO;
		self.emheaderHeight = - NavigationBarHeight;
		self.collectionView.backgroundColor = COLOR_WITH_HEX(0xFAFAFA);
		[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.edges.equalTo(@0);
		}];
		self.noFirstAutoRequest = YES;
		self.collectionView.showsVerticalScrollIndicator = NO;
		self.collectionView.showsHorizontalScrollIndicator = NO;
		[self.collectionView registerClass:[ShopCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class])];
		[self.collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ImageCollectionViewCell class])];
		[self.collectionView registerClass:[ListCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([ListCollectionReusableView class])];
		[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
	}
	return self;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
	if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
		if (indexPath.section == 1) {
			ListCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([ListCollectionReusableView class]) forIndexPath:indexPath];
			view.userInteractionEnabled = YES;
			return view;
		}
	}
	return [UICollectionReusableView new];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ImageCollectionViewCell class]) forIndexPath:indexPath];
		cell.backgroundColor = [UIColor redColor];
		return cell;
	} else if (indexPath.section == 1) {
		ShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class]) forIndexPath:indexPath];
		cell.model = [self.productList safeObjectAtIndex:indexPath.row];
		return cell;
	}
	UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
	return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
	return UIEdgeInsetsMake(0, kQKFitModuleDecimal(10.0), 0, kQKFitModuleDecimal(10.0));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	[collectionView deselectItemAtIndexPath:indexPath animated:YES];
	if (indexPath.section == 1) {
		
	}
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	if (section == 1) {
		return self.productList.count;
	}
	return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
	if (section == 1) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(34.0));
	} 
	return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(160.0));
	} else if (indexPath.section == 1) {
		CGFloat width = (ScreenWidth - kQKFitModuleDecimal(24 + 9)) / 2;
		return CGSizeMake(width,width + kQKFitModuleDecimal(94));
	}
	return CGSizeMake(0.0, 0.0);
}

- (void)sendRequestByID:(NSNumber *)pid {
	_pid = pid;
	[HttpAssottManager getCategoryListWithProductListWithCategoryID:pid WithSuccess:^(AssortCategroyModel *assort) {
		
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
	
	[HttpAssottManager getCategoryProductListWithCategoryID:pid andPageIndex:@(self.pageIndex) andSortType:@(1) withSuccess:^(NSArray *productList) {
		dispatch_async(dispatch_get_main_queue(), ^{
			self.productList = productList.copy;
			[self finishLoadData:productList];
			[self.collectionView reloadData];
		});
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)sendRequest {
	[self sendRequestByID:self.pid];
}

@end
